package com.cn.yz.common.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;

public class JsonUtils {

	private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);
	
	public static String toJsonString(Object object) {
		String str = JSONObject.toJSONString(object);
		LOG.info("JsonUtils.toJsonString---->str:"+str);
		return str;
	}
	
	public static <T> T toBean(String jsonString, Class<T> clazz) {
		if(clazz == null || StringUtils.isEmpty(jsonString)){
			return null;
		}
		LOG.info("JsonUtils.toJsonString---->jsonString:"+jsonString);
		LOG.info("JsonUtils.toJsonString---->clazz:"+clazz.getName());
		return (T)JSONObject.parseObject(jsonString, clazz);
	}
	
	public static void main(String[] args) {
		TestBean bean = new TestBean();
		bean.setId(1);
		bean.setName("123");
		String jsonString =  JsonUtils.toJsonString(bean);
		TestBean map = JsonUtils.toBean("1",TestBean.class);
		System.out.println(map.getId());
	}
}
